package com.jinbm.common.request;

import com.jinbm.common.ResultEnum;
import lombok.Data;

/**
 * 统一返回结果封装
 *
 * @author Jinbm
 * @date 2024/7/8
 * @since 1.0
 */
@Data
public class Result {

    private static final Result SUCCESS = new Result(ResultEnum.SUCCESS.code, ResultEnum.SUCCESS.msg, null);
    private static final Result ERROR = new Result(ResultEnum.SERVER_ERROR.code, ResultEnum.SERVER_ERROR.msg, null);
    private static final Result FAIL = new Result(ResultEnum.FAIL.code, ResultEnum.FAIL.msg, null);

    private Integer code;    // 返回状态码
    private String msg;     // 返回提示消息
    private Object data;    // 返回数据

    // 有参私有构造方法，方便后续赋值
    public Result(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 静态工厂方法
     *
     * @author Jinbm
     * @date 2024/7/8
     * @since 1.0
     * @return Result
     */
    public static Result success() {return SUCCESS;}
    public static Result success(String msg) {
        SUCCESS.setMsg(msg);
        return SUCCESS;
    }

    public static Result success(Object data) {
        SUCCESS.setData(data);
        return SUCCESS;
    }

    public static Result success(String msg,Object data){
        SUCCESS.setMsg(msg);
        SUCCESS.setData(data);
        return SUCCESS;
    }

    public static Result error(){return ERROR;}

    public static Result error(Object data) {
        ERROR.setData(data);
        return ERROR;
    }

    public static Result error(String msg){
        ERROR.setMsg(msg);
        return ERROR;
    }

    public static Result error(Integer code,String msg){
        ERROR.setCode(code);
        ERROR.setMsg(msg);
        return ERROR;
    }

    public static Result error(Integer code,String msg, Object data){
        ERROR.setCode(code);
        ERROR.setMsg(msg);
        ERROR.setData(data);
        return ERROR;
    }

    public static Result fail() {return FAIL;}

    public static Result fail(Object data) {
        FAIL.setData(data);
        return FAIL;
    }

    public static Result fail(String msg,Object data){
        FAIL.setMsg(msg);
        FAIL.setData(data);
        return FAIL;
    }
    public static Result fail(Integer code,String msg, Object data){
        FAIL.setCode(code);
        FAIL.setMsg(msg);
        FAIL.setData(data);
        return FAIL;
    }
}
